[XEND] XendVDI saves configuration on change.
authorAlastair Tse <atse@xensource.com>
Fri, 13 Oct 2006 14:34:01 +0000 (15:34 +0100)
committerAlastair Tse <atse@xensource.com>
Fri, 13 Oct 2006 14:34:01 +0000 (15:34 +0100)
Added a base class called AutoSaveObject that will attempt to call
save_config() if any attribute in the object changes. It isn't
particularly efficient, but we do not expect VDI to change much.

Signed-off-by: Alastair Tse <atse@xensource.com>
tools/python/scripts/xapi.py
tools/python/xen/xend/XendVDI.py

index e44663bd1bbf07f9dcb8eefe8c7c354afd517ce0..5b0d57132430c7649f722616a17412b37423461e 100644 (file)
@@ -37,6 +37,7 @@ COMMANDS = {
     'sr-list':   ('', 'List all SRs'),
     'vbd-create': ('<domname> <pycfg>', 'Create VBD attached to domname'),
     'vdi-list'  : ('', 'List all VDI'),
+    'vdi-rename': ('<vdi_uuid> <new_name>', 'Rename VDI'),
     'vdi-delete': ('<vdi_uuid>', 'Delete VDI'),
     'vif-create': ('<domname> <pycfg>', 'Create VIF attached to domname'),
 
@@ -303,6 +304,17 @@ def xapi_vdi_delete(*args):
     print 'Deleting VDI %s' % vdi_uuid
     result = execute(server.VDI.destroy, session, vdi_uuid)
     print 'Done.'
+
+def xapi_vdi_rename(*args):
+    server, session = _connect()
+    if len(args) < 2:
+        raise OptionError('Not enough arguments')
+
+    vdi_uuid = args[0]
+    vdi_name = args[1]
+    print 'Renaming VDI %s to %s' % (vdi_uuid, vdi_name)
+    result = execute(server.VDI.set_name_label, session, vdi_uuid, vdi_name)
+    print 'Done.'
     
         
 #
index 652585adcc97a6ee1430de9fbccda59f5582a4fe..c283a3a2ede666b78063184ad937a919f6f9b60c 100644 (file)
@@ -27,7 +27,25 @@ from xmlrpclib import dumps, loads
 KB = 1024
 MB = 1024 * 1024
 
-class XendVDI:
+class AutoSaveObject(object):
+    
+    def __init__(self):
+        self.cfg_path = None
+        self.auto_save = True
+        object
+
+    def save_config(self, cfg_file = None):
+        raise NotImplementedError()
+    
+    def __setattr__(self, name, value):
+        """A very simple way of making sure all attribute changes are
+        flushed to disk.
+        """
+        object.__setattr__(self, name, value)
+        if name != 'auto_save' and getattr(self, 'auto_save', False):
+            self.save_config()
+
+class XendVDI(AutoSaveObject):
     """Generic Xen API compatible VDI representation.
 
     @cvar SAVED_CFG: list of configuration attributes to save.
@@ -60,20 +78,20 @@ class XendVDI:
         self.read_only = False
         self.type = "system"
 
-        self.cfg_path = None
-
     def load_config_dict(self, cfg):
         """Loads configuration into the object from a dict.
 
         @param cfg: configuration dict
         @type  cfg: dict
         """
+        self.auto_save = False
         for key in self.SAVED_CFG:
             if key in cfg:
                 if key in self.SAVED_CFG_INT:
                     setattr(self, key, int(cfg[key]))
                 else:
                     setattr(self, key, cfg[key])
+        self.auto_save = True
 
     def load_config(self, cfg_path):
         """Loads configuration from an XMLRPC parameter format.
@@ -128,10 +146,12 @@ class XendQCOWVDI(XendVDI):
     def __init__(self, uuid, sr_uuid, qcow_path, image_path, cfg_path,
                  vsize, psize):
         XendVDI.__init__(self, uuid, sr_uuid)
+        self.auto_save = False
         self.qcow_path = qcow_path
         self.image_path = image_path
         self.cfg_path = cfg_path
         self.physical_utilisation = psize
         self.virtual_size = vsize
         self.sector_size = 1
+        self.auto_save = True